import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;
import java.io.FileWriter;
import java.io.IOException;

class Worker {
    private String id;
    private String name;
    private char gender;
    private String education;
    private int age;
    private double salary;


    public Worker(String id, String name, char gender, String education, int age, double salary) {
        this.id = id;
        this.name = name;
        this.gender = gender;
        this.education = education;
        this.age = age;
        this.salary = salary;
    }

    // Getters and Setters
    public String getId() { return id; }
    public void setId(String id) { this.id = id; }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public char getGender() { return gender; }
    public void setGender(char gender) { this.gender = gender; }
    public String getEducation() { return education; }
    public void setEducation(String education) { this.education = education; }
    public int getAge() { return age; }
    public void setAge(int age) { this.age = age; }
    public double getSalary() { return salary; }
    public void setSalary(double salary) { this.salary = salary; }


    @Override
    public String toString() {
        return "工号: " + id + ", 姓名: " + name + ", 性别: " + gender + ", 学历: " + education + ", 年龄: " + age + ", 工资: " + salary ;
    }
}

class WorkerList {
    private ArrayList<Worker> workers = new ArrayList<>();

    public boolean addWorker(Worker worker) {
        if (findWorkerById(worker.getId())) {
            System.out.println("工号已存在，添加失败！");
            return false;
        }
        workers.add(worker);
        System.out.println("职工添加成功！");
        return true;
    }

    public boolean modifyWorker(String id, Worker newWorker) {
        for (int i = 0; i < workers.size(); i++) {
            if (workers.get(i).getId().equals(id)) {
                workers.set(i, newWorker);
                System.out.println("职工信息修改成功！");
                return true;
            }
        }
        System.out.println("未找到工号为 " + id + " 的职工，修改失败！");
        return false;
    }

    public boolean deleteWorker(String id) {
        boolean removed = workers.removeIf(worker -> worker.getId().equals(id));
        if (removed) {
            System.out.println("工号为 " + id + " 的职工已删除！");
        } else {
            System.out.println("未找到工号为 " + id + " 的职工，删除失败！");
        }
        return removed;
    }

    public void sortWorkers(Comparator<Worker> comparator) {
        workers.sort(comparator);
        System.out.println("职工信息排序完成！");
    }

    public boolean findWorkerById(String id) {
        for (Worker worker : workers) {
            if (worker.getId().equals(id)) {
                System.out.println("找到职工信息：" + worker);
                return true;
            }
        }
        System.out.println("未找到工号为 " + id + " 的职工！");
        return false;
    }

    public void showAllWorkers() {
        if (workers.isEmpty()) {
            System.out.println("当前没有职工信息！");
        } else {
            for (Worker worker : workers) {
                System.out.println(worker);
            }
        }
    }

    public void saveToFile(String filename) {
        try (FileWriter writer = new FileWriter(filename)) {
            for (Worker worker : workers) {
                writer.write(worker.toString() + "\n");
            }
            System.out.println("职工信息已保存到文件 " + filename);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        WorkerList workerList = new WorkerList();

        while (true) {
            System.out.println("职工信息管理系统主菜单");
            System.out.println("1、职工信息添加");
            System.out.println("2、职工信息修改");
            System.out.println("3、职工信息删除");
            System.out.println("4、职工信息排序");
            System.out.println("5、职工信息查找");
            System.out.println("6、职工信息显示");
            System.out.println("7、职工信息保存");
            System.out.println("0、退出");
            System.out.print("请选择数字编号 0~7：");

            int choice = scanner.nextInt();
            scanner.nextLine();
            switch (choice) {
                case 1:
                    System.out.print("工号：");
                    String id = scanner.nextLine();
                    System.out.print("姓名：");
                    String name = scanner.nextLine();
                    System.out.print("性别（m 或 f）：");
                    char gender = scanner.nextLine().charAt(0);
                    System.out.print("学历：");
                    String education = scanner.nextLine();
                    System.out.print("年龄：");
                    int age = scanner.nextInt();
                    System.out.print("工资：");
                    double salary = scanner.nextDouble();
                    scanner.nextLine(); // Consume newline


                    Worker worker = new Worker(id, name, gender, education, age, salary);
                    workerList.addWorker(worker);
                    break;
                case 2:
                    System.out.print("请输入要修改的职工工号：");
                    String modifyId = scanner.nextLine();
                    System.out.print("新姓名：");
                    String newName = scanner.nextLine();
                    System.out.print("新性别（m 或 f）：");
                    char newGender = scanner.nextLine().charAt(0);
                    System.out.print("新学历：");
                    String newEducation = scanner.nextLine();
                    System.out.print("新年龄：");
                    int newAge = scanner.nextInt();
                    System.out.print("新工资：");
                    double newSalary = scanner.nextDouble();
                    scanner.nextLine(); // Consume newline

                    Worker newWorker = new Worker(modifyId, newName, newGender, newEducation, newAge, newSalary);
                    workerList.modifyWorker(modifyId, newWorker);
                    break;
                case 3:
                    System.out.print("请输入要删除的职工工号：");
                    String deleteId = scanner.nextLine();
                    workerList.deleteWorker(deleteId);
                    break;
                case 4:
                    System.out.println("请选择排序方式：");
                    System.out.println("1、按工号排序");
                    System.out.println("2、按工资排序");
                    int sortChoice = scanner.nextInt();
                    scanner.nextLine(); // Consume newline
                    if (sortChoice == 1) {
                        workerList.sortWorkers(Comparator.comparing(Worker::getId));
                    } else if (sortChoice == 2) {
                        workerList.sortWorkers(Comparator.comparing(Worker::getSalary));
                    } else {
                        System.out.println("无效的选择！");
                    }
                    break;
                case 5:
                    System.out.print("请输入要查找的职工工号：");
                    String findId = scanner.nextLine();
                    workerList.findWorkerById(findId);
                    break;
                case 6:
                    workerList.showAllWorkers();
                    break;
                case 7:
                    workerList.saveToFile("workers.txt");
                    break;
                case 0:
                    System.out.println("退出系统，再见！");
                    System.exit(0);
                default:
                    System.out.println("无效的选择，请重新选择。");
            }
        }
    }
}

